package com.tuanzi.loan.web.bean.statistics;

import com.base.common.http.HttpClientUtil;
import com.base.common.http.model.RequestHeader;
import com.base.common.log.LogService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.statistics.LoanWholeProcessCheckedStatRequest;
import com.tuanzi.loan.vo.response.statistics.LoanWholeProcessCheckedStatResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.LoanWholeProcessCheckedStatLazyDataModel;
import com.tuanzi.server.loan_base.constant.WholeAPIEnum;
import com.tuanzi.server.loan_base.util.DateUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanWholeProcessCheckedStatListBean
        extends JSFListingBean<LoanWholeProcessCheckedStatRequest,LoanWholeProcessCheckedStatResponse>{
    private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    @Value("${loan.wholeprocess.stat.url}")
    private String url;

    @PostConstruct
    public void init(){
        InitLazyDataModel();
    }

    private void InitLazyDataModel(){
        $ = new LoanWholeProcessCheckedStatRequest();
        try {
            $.setEndDate(format.parse(DateUtils.getCurrentDate()));
            $.setStartDate(format.parse(DateUtils.getCurrentDate()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        lazyDataModel =  new LoanWholeProcessCheckedStatLazyDataModel($);
    }


    public void onReset(){
        InitLazyDataModel();
    }

    public void postRresh(){
        try {
            HttpClientUtil.get(new RequestHeader(),String.format(url, DateUtils.getCurrentDate()));
            addInfoMessage("刷新缓存成功！");
        } catch (Exception e) {
            addErrorMessage("刷新失败，请联系管理员");
            LogService.error("刷新缓存出错",e);
        }
    }

    public WholeAPIEnum[] queryApi(){
        return WholeAPIEnum.values();
    }

    public String OwnnerNameById(String id){
        try {
            return WholeAPIEnum.getName(Integer.parseInt(id));
        } catch (NumberFormatException e) {
            return "全部";
        }
    }

    public String transLateChannel(String channel){
        try {
            return translateProductChannel(Integer.parseInt(channel));
        } catch (NumberFormatException e) {
            return "全部";
        }
    }

    public String transLateProduct(String productId){
        try {
            return translateProduct(Integer.parseInt(productId));
        } catch (NumberFormatException e) {
            return "全部";
        }
    }


}
